<template>
    <Content>
        <Card>
            <div  style="margin-bottom: 15px;padding-bottom: 10px; border-bottom: 1px solid #eee">
                <Row>
                    <p style="font-weight: bold; display: inline-block" >稿件推送终端</p>
                    <Button type="success" style="float: right" @click="pushData">推送</Button>

                </Row>
                <Row>
                    <Checkbox style="display: inline-block;font-size: 14px" :indeterminate="indeterminate" :value="checkAll" @click.prevent.native="handleCheckAll">全选</Checkbox>
                    <CheckboxGroup v-model="checkAllGroup" @on-change="checkAllGroupChange" style="display: inline-block;" >
                        <Checkbox :label="item.id" :key="index" v-for="(item,index) in manuscriptPushTypeList" v-if="index<=3" style="font-size: 14px">{{item.manuscriptPushTypeName}}</Checkbox>
                        <Checkbox disabled :label="item.id" :key="index" v-for="(item,index) in manuscriptPushTypeList" v-if="index>3" style="font-size: 14px">{{item.manuscriptPushTypeName}}</Checkbox>

                    </CheckboxGroup>
                </Row>

            </div>


            <Table size="large" :loading="loading" :columns="manuscriptColumn" :data="manuscriptData" @on-selection-change="selectedID"></Table>
            <div style="margin-top:10px;text-align: center">
                <Page :total="total" :current.sync="currentPage" :page-size="pageSize" @on-change="changeManuPage" show-total></Page>
            </div>

        </Card>
    </Content>
</template>

<script>
    export default {
        name: "ManuscriptPush",
        inject:['reload'],
        data(){
            return{
                loading:false,
                pageNum:1,//页码，默认初始值是1
                pageSize:5,//每页显示的数量
                total:0,
                currentPage:1,
                indeterminate:false,
                checkAll:false,
                checkAllGroup:[],
                manuscriptIdList:[],//push文章id
                manuscriptPushTypeIdList:[],//push的类型Id
                manuscriptPushTypeList:[],
                manuscriptPushTypeIdData:[],
                manuscriptPushTypeTotal:0,
                manuscriptColumn:[
                    {
                        type:'selection',
                        width:50,
                        align:'center'

                    },
                    {
                        title:'文章名称',
                        key:'title',
                        align:'center'
                    },
                    {
                        title:'作者',
                        key:'author',
                        align:'center'

                    },
                    {
                        title:'时间',
                        key:'publishTime',
                        align:'center'

                    }
                ],
                manuscriptData:[]

            }
        },
        watch:{
            checkAllGroup(){
            }
        },
        methods:{
            //搜索稿件推送类型信息
            getManuscriptPushType(){
                if(this.loading) return;
                this.loading=true
                this.$http({
                    method:'POST',
                    url:this.$url.baseUrl+'/consulting-library/api/manuscript/queryManuscriptPushType',
                    data:{}
                }).then(response =>{
                    let result=response.data
                     if(result.meta.success){
                         this.manuscriptPushTypeList=result.data.list
                         for(let i=0;i<this.manuscriptPushTypeList.length;i++){
                             // console.log(this.manuscriptPushTypeList[i].id)
                             this.manuscriptPushTypeIdData.push(this.manuscriptPushTypeList[i].id)
                         }
                         this.manuscriptPushTypeTotal=result.data.total
                     }
                     this.loading=false
                    // console.log(response.data)
                })
            },
            //UPLOAD是稿件上传的类型，获取已经上传的稿件信息
            getManuscriptData(){
                this.$http({
                    method: 'POST',
                    url:this.$url.baseUrl+'/consulting-library/api/manuscript/queryManuscript',
                    data:{
                        'pageNum': this.pageNum,
                        'pageSize': this.pageSize,
                        'sortType':'desc',
                        'sortKey':'publishTime',
                        'paramModel':{
                            'statusEnum':'UPLOAD'
                        }
                    }
                }).then(response =>{
                    let result =response.data
                    if(result.meta.success){
                        this.manuscriptData=result.data.list
                        this.total=result.data.total
                    }
                })
            },
            //全选
            handleCheckAll(){
                if(this.indeterminate){
                    this.checkAll = false
                }else{
                    this.checkAll = !this.checkAll
                }
                this.indeterminate = false;

                if(this.checkAll){
                    this.checkAllGroup=this.manuscriptPushTypeIdData
                }else{
                    this.checkAllGroup=[]
                }
            },
            checkAllGroupChange(data){
                // console.log(this.checkAllGroup)
                if(data.length==this.manuscriptPushTypeTotal){
                    this.indeterminate=false;
                    this.checkAll=true;
                }else if(data.length>0){
                    this.indeterminate=true;
                    this.checkAll=false;
                }else{
                    this.indeterminate=false;
                    this.checkAll=false;
                }
            },
            //推送已选择的稿件
            pushData(){
                this.manuscriptPushTypeIdList= this.checkAllGroup
                // console.log(this.checkAllGroup)
                // console.log(this.manuscriptIdList)
                if(this.checkAllGroup.length==0||this.manuscriptIdList.length==0){
                    this.$Message.error("请选择要推送的终端类型或者文章")
                }
                this.$http({
                    method: 'POST',
                    url:this.$url.baseUrl+'/consulting-library/api/manuscript/insertManuscriptPushBatch',
                    data:{
                        "manuscriptIdList": this.manuscriptIdList,
                        "manuscriptPushTypeIdList": this.checkAllGroup,
                        "userId": 1
                    }

                }).then(response =>{
                    if(response.data.meta.success){
                        this.$Message.success("文章推送成功")
                        this.reload()
                    }
                    // console.log(response.data)
                })
            },
            //获取选择文章的ID
            selectedID(selection){
                this.manuscriptIdList=[]
                for (let i in selection) {
                    this.manuscriptIdList.push(selection[i].id)

                }
                // console.log(this.manuscriptIdList)

            },
            //分页
            changeManuPage(index){
                this.pageNum=index
                this.getManuscriptData()
            }

        },
        mounted(){

            this.getManuscriptPushType()
            this.getManuscriptData()
        }
    }
</script>

<style scoped>

</style>